-module(simple_cache).

-export([
    insert/2,
    lookup/1,
    delete/1
    ]).

insert(Key, Value) ->
    case sc_store:lookup(Key) of 
        {ok, Pid} ->
            sc_element:replace(Pid, Value);
        {error, _} ->
            {ok, Pid} = sc_element:create(Value),
            sc_store:insert(Key, Pid)
    end.

lookup(Key) ->
    try 
        {ok, Pid} = sc_store:lookup(Key),
        Value = sc_element:fetch(Pid),
        {ok, Value}
    catch 
        _ErrClass:_ ->
            {error, not_exist}
    end.

delete(Key) ->
    case sc_store:lookup(Key) of 
        {ok, Pid} ->
            sc_element:delete(Pid);
        {error, _} ->
            skip
    end,
    ok.